<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>
    PHP Builder 建造者模式 |
    
    无敌小笼包</title>
  
    <link rel="shortcut icon" href="/favicon.png">
  
  <link rel="stylesheet" href="/css/style.css">
  
    <link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">
  
  <script src="/js/pace.min.js"></script>
</head>

<body>
<main class="content">
  <section class="outer">
  <article id="post-Builder" class="article article-type-post" itemscope itemprop="blogPost">

  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      PHP Builder 建造者模式
    </h1>
  

      </header>
    

    
      <div class="article-meta">
        <a href="/2017/02/22/Builder/" class="article-date">
  <time datetime="2017-02-22T02:20:12.000Z" itemprop="datePublished">2017-02-22</time>
</a>
        
  <div class="article-category">
    <a class="article-category-link" href="/categories/Skill/">Skill</a>
  </div>

      </div>
    

    <div class="article-entry" itemprop="articleBody">
      
      
      
        <ul>
<li>建造者设计模式的母的是消除其他对象的复杂创建过程</li>
</ul>
<p>使用建造者设计模式不仅是最佳的做法，而且在某个对象的构造和配置方法改变时尽可能地减少重复的代码</p>
<a id="more"></a>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="comment">/**</span></div><div class="line"> * Builder  建造者模式</div><div class="line"> * -------------------------------------</div><div class="line"> * ** 来自说明 **</div><div class="line"> *</div><div class="line"> * 建造者设计模式的母的是消除其他对象的复杂创建过程，</div><div class="line"> * 使用建造者设计模式不仅是最佳的做法</div><div class="line"> * 而且在某个对象的构造和配置方法改变时尽可能地减少重复的代码</div><div class="line"> * </div><div class="line"> * ===================================== </div><div class="line"> * ** 应用场景 **</div><div class="line"> *</div><div class="line"> * 数据库接口类 | 优化基类</div><div class="line"> * </div><div class="line"> * -------------------------------------</div><div class="line"> * </div><div class="line"> * <span class="doctag">@version</span> $&#123;Id&#125;$</div><div class="line"> * <span class="doctag">@author</span> Shaowei Pu &lt;54268491<span class="doctag">@qq</span>.com&gt;</div><div class="line"> */</div></pre></td></tr></table></figure>
<hr>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// 现在是这样一个情况，您是学生个人档案录入员，你写了这样一个类</span></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Entering</span> </span>&#123;</div><div class="line"></div><div class="line">	<span class="keyword">public</span> $info = [];</div><div class="line">	<span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">setName</span><span class="params">( $name )</span></span>&#123;</div><div class="line">		<span class="keyword">$this</span>-&gt;info[<span class="string">'name'</span>] = $name;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	<span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">setOld</span><span class="params">( $old )</span></span>&#123;</div><div class="line">		<span class="keyword">$this</span>-&gt;info[<span class="string">'old'</span>] = $old;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	<span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">setGender</span><span class="params">( $gender )</span></span>&#123;</div><div class="line">		<span class="keyword">$this</span>-&gt;info[<span class="string">'gander'</span>]  = $gender;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"><span class="comment">// 然后你看到了你们Boss 在许多年前，你还是一个乳臭未干的小孩时写的录入类 </span></div><div class="line">$worker = <span class="keyword">new</span> Entering;</div><div class="line">$worker-&gt;setName(<span class="string">'jacky'</span>);</div><div class="line">$worker-&gt;setOld(<span class="string">'22'</span>);</div><div class="line">$worker-&gt;setGender(<span class="string">'男'</span>);</div><div class="line"><span class="comment">// 然后这样就可以了,一个学生的完整信息就这么弄出来了</span></div><div class="line">var_dump($worker-&gt;info);</div><div class="line"><span class="comment">/* </span></div><div class="line">+----------------------------------------------------------------------</div><div class="line">| array (size=3)</div><div class="line">|   'name' 	 =&gt; string 'jacky' (length=5)</div><div class="line">|   'old' 	 =&gt; string '22' (length=2)</div><div class="line">|   'gander' =&gt; string '男' (length=3)</div><div class="line">+----------------------------------------------------------------------</div><div class="line">*/</div><div class="line"></div><div class="line"><span class="comment">// 但是爱折腾的你从来不会放弃任何机会，自从学习了建造者模式的你 总有种蠢蠢欲动，于是乎就有了下面的类</span></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">EnteringBuilder</span></span>&#123;</div><div class="line">	</div><div class="line">	<span class="keyword">protected</span> $_baseObject = <span class="keyword">null</span>;</div><div class="line">	<span class="keyword">protected</span> $_newInfo    = []  ;</div><div class="line"></div><div class="line">	<span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">__construct</span><span class="params">( array $info )</span></span>&#123;</div><div class="line">		<span class="keyword">$this</span>-&gt;_baseObject = <span class="keyword">new</span> Entering;</div><div class="line">		<span class="keyword">$this</span>-&gt;_newInfo  = $info;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	<span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">build</span><span class="params">()</span></span>&#123;</div><div class="line">		<span class="keyword">$this</span>-&gt;_baseObject-&gt;setName  (  <span class="keyword">$this</span>-&gt;_newInfo[<span class="string">'name'</span>]);</div><div class="line">		<span class="keyword">$this</span>-&gt;_baseObject-&gt;setOld 	 (  <span class="keyword">$this</span>-&gt;_newInfo[<span class="string">'old'</span>]);</div><div class="line">		<span class="keyword">$this</span>-&gt;_baseObject-&gt;setGender(  <span class="keyword">$this</span>-&gt;_newInfo[<span class="string">'gander'</span>]);</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	<span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">getInfo</span><span class="params">()</span></span>&#123;</div><div class="line">		<span class="keyword">return</span> <span class="keyword">$this</span>-&gt;_baseObject-&gt;info;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 然后这样</span></div><div class="line">$new_worker = <span class="keyword">new</span> EnteringBuilder([</div><div class="line">			<span class="string">'name'</span> 	 =&gt; <span class="string">'lucy'</span>,</div><div class="line">			<span class="string">'old'</span> 	 =&gt;  <span class="number">22</span>,</div><div class="line">			<span class="string">'gander'</span> =&gt; <span class="string">'女'</span>,</div><div class="line">	]);</div><div class="line">$new_worker-&gt;build();</div><div class="line"><span class="comment">// 然后这样就可以了,一个学生的完整信息就这么弄出来了</span></div><div class="line">var_dump($new_worker-&gt;getInfo());</div><div class="line"><span class="comment">/* </span></div><div class="line">+----------------------------------------------------------------------</div><div class="line">|array (size=3)</div><div class="line">|  'name' =&gt; string 'lucy' (length=4)</div><div class="line">|  'old' =&gt; int 22</div><div class="line">|  'gander' =&gt; string '女' (length=3)</div><div class="line">+----------------------------------------------------------------------</div><div class="line">*/</div></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://blog.mango16.cc/2017/02/22/Builder/" data-id="cjrbtw1xa00016d8hinrvxu78"
         class="article-share-link">分享</a>
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Design-Pattern/">Design Pattern</a></li></ul>

    </footer>

  </div>

  
    
  <nav class="article-nav">
    
      <a href="/2017/02/23/Decorator/" class="article-nav-link">
        <strong class="article-nav-caption">前一篇</strong>
        <div class="article-nav-title">
          
            PHP Decorator 装饰器模式
          
        </div>
      </a>
    
    
      <a href="/2017/02/21/phpAdapter/" class="article-nav-link">
        <strong class="article-nav-caption">后一篇</strong>
        <div class="article-nav-title">PHP 适配器模式(Adapter Design Pattern)</div>
      </a>
    
  </nav>


  

  
    
  <div class="gitalk" id="gitalk-container"></div>
  <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
  <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
  <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
  <script type="text/javascript">
      var gitalk = new Gitalk({
        clientID: 'caba8826a61790936307',
        clientSecret: '09a15037dcab30d9adf35a53ec6961939169f797',
        repo: 'm9rco.github.io',
        owner: 'm9rco',
        admin: ['m9rco'],
        // id: location.pathname,      // Ensure uniqueness and length less than 50
        id: md5(location.pathname),
        distractionFreeMode: false  // Facebook-like distraction free mode
      })

  gitalk.render('gitalk-container')
  </script>

  

</article>



</section>
  <footer class="footer">
  
  <div class="outer">
    <ul class="list-inline">
      <li>&copy; 2019 无敌小笼包</li>
      <i class="pulse"></i>
    </ul>
  </div>
</footer>

</main>
<aside class="sidebar">
  <button class="navbar-toggle"></button>

<nav class="navbar">
  
    <div class="logo">
      <a href="/"><img src="/images/hexo.svg" alt="无敌小笼包"></a>
    </div>
  
  <ul class="nav nav-main">
    
      <li class="nav-item">
        <a class="nav-item-link" href="/">主页</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/archives">归档</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/gallery">相册</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/nagging">叽咕</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/about">关于</a>
      </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="Search">
        <i class="fe fe-search"></i>
        搜索
      </a>
    </li>
  </ul>
</nav>

<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
        <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
          <i class="fe fe-feed"></i>
        </a>
      
    </li>
  </ul>
</nav>

<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
</aside>
  <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/lazyload.min.js"></script>


  <script src="/fancybox/jquery.fancybox.min.js"></script>



  <script src="/js/search.js"></script>


<script src="/js/ocean.js"></script>

</body>
</html>